package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import java.util.List;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.City;
import com.shaoshaohuo.app.entity.News;
import com.shaoshaohuo.app.entity.Province;
import com.shaoshaohuo.app.entity.ToolApp;
import com.shaoshaohuo.app.entity.ToolsEntity;
import com.shaoshaohuo.app.entity.ToolsEntity.Tools;
import com.shaoshaohuo.app.entity.Weather;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.adapter.NewsAdapter;
import com.shaoshaohuo.app.adapter.ToolAppAdapter;
import com.shaoshaohuo.app.view.MeasureGridView;
import com.shaoshaohuo.app.view.MeasureListView;
import com.shaoshaohuo.app.view.TopbarView;

public class ToosActivity extends BaseActivity {

    private TopbarView mTopbarView;

    private TextView mCityText;
    private TextView mWeatherDetailText;
    private MeasureGridView mToolsGv;
    private MeasureListView mListView;

    private int newssize = 5;
    private String citycode = "010";

    private NewsAdapter<News> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools);

        initView();
        setUpView();
        requestData();
    }

    private void requestData() {
        startLoadingDialog();
        RequestService.getInstance().getTools(this, newssize + "", citycode, ToolsEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    ToolsEntity entity = (ToolsEntity) resultData;
                    updateUi(entity);
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    protected void updateUi(ToolsEntity entity) {
        Tools data = entity.getData();
        Weather weather = data.getWeather();
        List<ToolApp> apps = data.getApps();
        List<News> news = data.getNews();

        if (weather != null) {
            mCityText.setText(weather.getCityname() + "天气（切换）");
            mWeatherDetailText.setText(weather.getCondition()+" "+weather.getTemp()+" "+weather.getWind());
        }
        if (apps != null) {
            if (mAdapter == null) {
                mAdapter = new NewsAdapter<News>(this, news, false);
                mListView.setAdapter(mAdapter);
            } else {
                mAdapter.setData(news);
            }
        }
        if (news != null) {
            ToolAppAdapter<ToolApp> serviceAdapter = new ToolAppAdapter<ToolApp>(this, apps, false);
            mToolsGv.setAdapter(serviceAdapter);
        }

    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
    }

    private void setUpView() {
        mTopbarView.setCenterText("工具箱");
        mTopbarView.setLeftView(true, true);

        mCityText = (TextView) this.findViewById(R.id.textview_weather_city);
        mWeatherDetailText = (TextView) this.findViewById(R.id.textview_weather_detail);
        mToolsGv = (MeasureGridView) this.findViewById(R.id.gv_tool_list);
        mListView = (MeasureListView) this.findViewById(R.id.listview_news_list);
        
        mToolsGv.setOnItemClickListener(new OnItemClickListener() {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToolApp news = (ToolApp) parent.getItemAtPosition(position);
                Intent aboutIntent = new Intent(ToosActivity.this,WebBrowserActivity.class);
                aboutIntent.putExtra(WebBrowserActivity.TITLE, news.getName());
                aboutIntent.putExtra(WebBrowserActivity.URL, news.getUrl());
                startActivity(aboutIntent);
            }
        });
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) parent.getItemAtPosition(position);
                Intent aboutIntent = new Intent(ToosActivity.this,WebBrowserActivity.class);
                aboutIntent.putExtra(WebBrowserActivity.TITLE, news.getTitle());
                aboutIntent.putExtra(WebBrowserActivity.URL, news.getShowurl());
                startActivity(aboutIntent);
            }
        });
        
        mCityText.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ToosActivity.this,SelectProvinceActivity.class);
                startActivityForResult(intent, 0);
            }
        });
        
    }
    
    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg1 == RESULT_OK && arg2 != null) {

                Province startProvince = (Province) arg2.getSerializableExtra(ExtraName.province);
                City startCity = (City) arg2.getSerializableExtra(ExtraName.city);
                mCityText.setText(startCity.getCityname()+"天气（切换）");

                citycode = startCity.getCitycode();
                
                requestData();
                
        }
    }

}
